package cn.juque.systemservice.service.provider;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.juque.common.annotation.HandlerAppException;
import cn.juque.common.base.BaseResponseDTO;
import cn.juque.common.base.PageInfo;
import cn.juque.common.constants.MessageEnum;
import cn.juque.common.thread.BaseOperatorThreadLocal;
import cn.juque.common.utils.ValidatorUtil;
import cn.juque.systemservice.api.dto.gen.*;
import cn.juque.systemservice.api.provider.IGenInfoProvider;
import cn.juque.systemservice.cache.GenInfoCache;
import cn.juque.systemservice.entity.GenInfo;
import cn.juque.systemservice.entity.GenMxInfo;
import cn.juque.systemservice.service.IGenInfoService;
import cn.juque.systemservice.service.IGenMxInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author JUQUE
 * @version 1.0.0
 * <li>simple-generator</li>
 * <li></li>
 * @date 2020-04-01 20:32:42
 **/
@Service("genInfoProvider")
@Slf4j
@HandlerAppException
public class GenInfoProviderImpl implements IGenInfoProvider {

    @Resource
    private BaseOperatorThreadLocal baseOperatorThreadLocal;

    @Resource
    private IGenInfoService genInfoService;

    @Resource
    private IGenMxInfoService genMxInfoService;

    @Resource
    private GenInfoCache genInfoCache;

    @Resource
    private ValidatorUtil validatorUtil;

    /**
     * 列表检索
     *
     * @param searchDto 入参
     * @return List
     */
    @Override
    public BaseResponseDTO<List<GenDetailDTO>> listGen(GenSearchDTO searchDto) {
        this.validatorUtil.checkNotNull(searchDto);
        PageInfo pageInfo = searchDto.getPageInfo();
        GenInfo sysGen = new GenInfo();
        BeanUtil.copyProperties(searchDto, sysGen);
        List<GenInfo> genList;
        if (null == pageInfo) {
            genList = this.genInfoService.listGenWithoutPage(sysGen);
        } else {
            genList = this.genInfoService.listGenWithPage(pageInfo, sysGen);
        }
        List<GenDetailDTO> genDtoList = BeanUtil.copyToList(genList, GenDetailDTO.class);
        return new BaseResponseDTO<>(MessageEnum.OK, genDtoList, pageInfo);
    }

    /**
     * 根据主键id检索明细列表
     *
     * @param genId 入参
     * @return list
     */
    @Override
    public BaseResponseDTO<List<GenMxDTO>> listGenMx(String genId) {
        GenMxInfo query = new GenMxInfo();
        query.setGenId(genId);
        List<GenMxInfo> list = this.genMxInfoService.listGenMxWithoutPage(query);
        List<GenMxDTO> result = CollUtil.newArrayList();
        if (CollUtil.isNotEmpty(list)) {
            list.forEach(t -> {
                GenMxDTO mx = new GenMxDTO();
                BeanUtil.copyProperties(t, mx);
                result.add(mx);
            });
        }
        return new BaseResponseDTO<>(MessageEnum.OK, result);
    }

    /**
     * 新增
     *
     * @param sysGenDto 入参
     * @return Boolean
     */
    @Override
    public BaseResponseDTO<Boolean> addGen(AddGenDTO sysGenDto) {
        this.validatorUtil.fastValidate(sysGenDto);
        this.baseOperatorThreadLocal.set(sysGenDto.getBaseOperator());
        GenInfo sysGen = new GenInfo();
        BeanUtil.copyProperties(sysGenDto, sysGen);
        this.genInfoService.saveGen(sysGen);
        return new BaseResponseDTO<>(MessageEnum.OK, Boolean.TRUE);
    }

    /**
     * 更新
     *
     * @param sysGenDto 入参
     * @return Boolean
     */
    @Override
    public BaseResponseDTO<Boolean> updateGen(UpdateGenDTO sysGenDto) {
        this.validatorUtil.fastValidate(sysGenDto);
        this.baseOperatorThreadLocal.set(sysGenDto.getBaseOperator());
        GenInfo sysGen = new GenInfo();
        BeanUtil.copyProperties(sysGenDto, sysGen);
        this.genInfoService.updateGen(sysGen);
        return new BaseResponseDTO<>(MessageEnum.OK, Boolean.TRUE);
    }

    /**
     * 删除
     *
     * @param req 入参
     * @return Boolean
     */
    @Override
    public BaseResponseDTO<Boolean> deleteGen(GenDeleteDTO req) {
        List<String> genIdList = req.getIdList();
        this.genInfoService.deleteGen(genIdList);
        return new BaseResponseDTO<>(MessageEnum.OK, Boolean.TRUE);

    }

    /**
     * 获取新的计数
     *
     * @param dto 入场
     * @return BaseResponseDTO
     */
    @Override
    public BaseResponseDTO<Long> genIncrement(GenIncrementDTO dto) {
        this.validatorUtil.fastValidate(dto);
        long incrment;
        if (null == dto.getDate()) {
            incrment = this.genInfoCache.getGen(dto.getGenPrefix());
        } else {
            incrment = this.genInfoCache.getGen(dto.getGenPrefix(), dto.getDate());
        }
        return new BaseResponseDTO<>(MessageEnum.OK, incrment);
    }
}
